﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.IRepository.SystemSettings.InstituTions;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.PoModel.SystemSettings.InstituTions;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemSettings.InstituTions
{
    public class UserRepository : BaseRepository, IUserRepository
    {
        public UserRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {


        }
        public List<MajorsManagermentPo> BindMajors()
        {
            return _systemCenterDbSoaContext.MajorsManagermentPo.ToList();
        }

        public List<RolesManagermentPo> BindRoles()
        {
            return _systemCenterDbSoaContext.RolesManagermentPo.ToList();
        }
        public List<RolePo> BindRole()
        {
            return _systemCenterDbSoaContext.RolePo.ToList();
        }
        public List<DepartmentOfManagementPo> BindDep()
        {
            return _systemCenterDbSoaContext.DepartmentOfManagementPo.ToList();
        }

        public List<SetPositionPo> BindSetPosition()
        {
            return _systemCenterDbSoaContext.SetPositionPo.ToList();
        }

        public List<TechnicalPositionPo> BindTechnical()
        {
            return _systemCenterDbSoaContext.TechnicalPositionPo.ToList();
        }

        public UserManagermentPo GetUserMentById(int id)
        {
            return _systemCenterDbSoaContext.UserManagermentPo.FirstOrDefault(c => c.user_ids == id);
        }

        public PagerResult<UserManagermentPo> SearchPage(SearchModel searchModel)
        {
            var query = _systemCenterDbSoaContext.UserManagermentPo.AsQueryable();
            //判断有没有搜索条件
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                //把搜索条件拿出来
                Dictionary<string, string> tempDi = searchModel.SearchItem;
                //tempDi["user_name"]
                //搜索字段为user_name  
                string depIds = "depIds";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(depIds) && !string.IsNullOrEmpty(tempDi[depIds]))
                {
                    int aaa = 0;
                    if (int.TryParse(tempDi[depIds],out aaa))
                    {
                        //类似于like查询, 判断userName是否包含查询条件中的条件值
                        query = query.Where(c => c.user_depids.Equals(tempDi[depIds]));
                    }
                    
                }
                string userName = "userName";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(userName) && !string.IsNullOrEmpty(tempDi[userName]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    query = query.Where(c => c.user_names.Contains(tempDi[userName]));
                }
                string isleave = "isleave";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(isleave) && !string.IsNullOrEmpty(tempDi[isleave]))
                {
                    int aaa = 0;
                    if (int.TryParse(tempDi[isleave], out aaa))
                    {
                        //类似于like查询, 判断userName是否包含查询条件中的条件值
                        query = query.Where(c => c.user_isleave.Equals(tempDi[isleave]));
                    }
                }
            }
            return BasePageSearch(query, searchModel);
        }

        public bool UserMentAdd(UserManagermentPo view)
        {
            _systemCenterDbSoaContext.UserManagermentPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool UserMentDelete(string ids)
        {
            string[] arr = ids.Split(',');
            foreach (var item in arr)
            {
                int id = Convert.ToInt32(item);
                var list = _systemCenterDbSoaContext.UserManagermentPo.Find(id);
                _systemCenterDbSoaContext.UserManagermentPo.Remove(list);
            }
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool UserMentUpdate(UserManagermentPo view)
        {
            _systemCenterDbSoaContext.Entry(view).State = EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
    }
}
